package com.wmh.baseservice.operationaid.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.operationaid.entity.PddStoreData;
import com.wmh.baseservice.operationaid.mapper.PddStoreDataMapper;
import com.wmh.baseservice.operationaid.pojo.vo.store.PddStoreCommitDataReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.store.PddStoreListReqVO;
import com.wmh.baseservice.operationaid.service.PddStoreDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
* @author 谢骏
* @description 针对表【operation_aid_pdd_store_data】的数据库操作Service实现
* @createDate 2023-07-07 12:38:51
*/
@Service
@Slf4j
public class PddStoreDataServiceImpl extends ServiceImpl<PddStoreDataMapper, PddStoreData>
    implements PddStoreDataService {

    @Resource
    private PddStoreDataMapper pddStoreDataMapper;

    @Override
    public List<String> getStoreOptions() {
        return pddStoreDataMapper.getStoreOptions();
    }

    @Override
    public void batchRemove(List<String> ids) {
        remove(new LambdaQueryWrapperX<PddStoreData>()
                .in(PddStoreData::getId, ids));
    }

    @Override
    public List<PddStoreData> getPddStoreList(PddStoreListReqVO reqVO) {
        LambdaQueryWrapperX<PddStoreData> wrapper = new LambdaQueryWrapperX<PddStoreData>()
                .eq(StrUtil.isNotBlank(reqVO.getStoreName()), PddStoreData::getStoreName, reqVO.getStoreName());
        if (StrUtil.isAllNotBlank(reqVO.getStartDate(), reqVO.getEndDate())) {
            wrapper.between(PddStoreData::getDataDate,
                    LocalDateTimeUtil.parse(reqVO.getStartDate() + "T00:00:00"),
                    LocalDateTimeUtil.parse(reqVO.getEndDate() + "T00:00:00"));
        }

        return pddStoreDataMapper.selectList(wrapper);
    }

    @Override
    public void commit(PddStoreCommitDataReqVO reqVO) {
        PddStoreData entity = baseMapper.selectOne(new LambdaQueryWrapperX<PddStoreData>()
                .eq(PddStoreData::getDataDate, reqVO.getDataDate())
                .eq(PddStoreData::getStoreName, reqVO.getStoreName()));
        // 更新
        if (!Objects.isNull(entity)) {
            BeanUtils.copyProperties(reqVO, entity);
            // 新增
        } else {
            entity = new PddStoreData();
            BeanUtils.copyProperties(reqVO, entity);
            entity.setDataDate(LocalDateTimeUtil.parse(reqVO.getDataDate() + "T00:00:00"));
        }

        entity.setUpdateTime(null);
        saveOrUpdate(entity);
    }
}




